package com.hymjweb.demo.framework.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro非对称密码生成功工具
 */
public class ShiroUtil {

    // 密码加密盐值长度
    public static final int PWD_SALT_LENGTH = 6;

    // 密码加密算法
    public static final String PWD_ALGORITHM_NAME = "SHA-512";

    // 密码加密次数
    public static final int PWD_HASH_ITERATIONS = 2;

    /**
     * 生成密码
     * @param pwd
     * @param salt
     * @return
     */
    public static String generatePwdEncrypt(String pwd, String salt){
        SimpleHash hash = new SimpleHash(PWD_ALGORITHM_NAME, pwd, salt, PWD_HASH_ITERATIONS);
        return hash.toString();
    }

    public static String generateSalt(){
        return RandomStringUtils.randomAlphabetic(PWD_SALT_LENGTH);
    }

    public static void main(String[] args) {
        Map<String,String> pwd = new LinkedHashMap<>();
        for(int i=0;i<30;i++){
            String generateSalt = generateSalt();
            String generatePwdEncrypt = generatePwdEncrypt("admin",generateSalt);
            pwd.put(generateSalt,generatePwdEncrypt);
        }

        System.out.println(JSONObject.toJSON(pwd));

    }

}
